package com.wq.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wq.dto.SetmealDto;
import com.wq.entity.Setmeal;
import com.wq.entity.SetmealDish;
import com.wq.mapper.SetmealMapper;
import com.wq.service.SetmealDishService;
import com.wq.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ description
 * @ author wuyunpeng
 * @ create 2022-07-21 14:50
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    /**
     * 保存套餐，保存套餐关联的菜品信息
     *
     * @param setmealDto
     * @return
     */
    @Autowired
    SetmealDishService setmealDishService;

    @Override
    public void saveWithDishes(SetmealDto setmealDto) {
        this.save(setmealDto);
        Long setmealID = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            SetmealDish setmealDish = new SetmealDish();
            BeanUtils.copyProperties(item, setmealDish);
            setmealDish.setSetmealId(setmealID);
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
